<?php

/**
 * 账户充值
 */

namespace app\member\common;

class RechargeCommon extends \app\member\common\MemberCommon {

    protected function index() {
        if(!isPost()) {
            $this->setMeta('账户充值');
            $this->pageInfo['name'] = '账户充值';
            $payList = target('member/PayConfig')->typeList(true);
            $this->assign('payList', $payList);
            $this->memberDisplay();
        }else {
            $type = substr(request('', 'type', '', 'html_in'), 0, 15);
            $money = request('', 'money', 0, 'price_format');

            if(empty($type)) {
                $this->error('请选择支付方式!');
            }
            if($money <= 0) {
                $this->error('充值金额错误!');
            }
            $payList = target('member/PayConfig')->typeList();
            $payTypeInfo = $payList[$type];
            if (empty($payTypeInfo)) {
                $this->error('该支付类型不存在!');
            }
            if ($this->mobile) {
                if ($payTypeInfo['platform'] <> 'mobile') {
                    $this->error('该支付类型无法使用!');
                }
            } else {
                if ($payTypeInfo['platform'] <> 'web') {
                    $this->error('该支付类型无法使用!');
                }
            }

            $logNo = target('member/Finance', 'service')->logNo($this->userInfo['user_id']);
            $config = \Config::get('dux.use');
            $md5 = md5($logNo . $config['safe_key']);

            $model = target('member/MemberRecharge');
            $model->beginTransaction();
            $data = [];
            $data['user_id'] = $this->userInfo['user_id'];
            $data['money'] = $money;
            $data['recharge_no'] = $logNo;
            $data['status'] = 0;
            $data['create_time'] = time();
            if(!target('member/MemberRecharge')->add($data)) {
                $model->rollBack();
                $this->error('充值订单创建失败!');
            }

            $url = target($payTypeInfo['target'], 'pay')->getUrl([
                'user_id' => $this->userInfo['user_id'],
                'order_no' => $logNo,
                'money' => $money,
                'title' => '会员充值',
                'body' => '会员充值',
                'url' => url('member/Recharge/pay'),
                'app' => 'member',
            ], url('complete', ['pay_no' => $logNo, 'pay_md5' => $md5]));

            if (!$url) {
                $model->rollBack();
                $this->error(target($payTypeInfo['target'], 'pay')->getError());
            }
            $model->commit();
            $this->success('即将跳转到支付页面!', $url);
        }
    }

    protected function complete() {
        $payNo = request('get', 'pay_no', 0);
        $payMd5 = request('get', 'pay_md5', 0);
        if (empty($payNo) || empty($payMd5)) {
            $this->error404();
        }

        $config = \Config::get('dux.use');
        $md5 = md5($payNo . $config['safe_key']);
        if ($md5 <> $payMd5) {
            $this->error404();
        }

        $name = '充值成功';
        $desc = '充值完成,请等待系统处理!';

        $this->pageInfo['name'] = $name;
        $this->setMeta($name);
        $this->setCrumb([
            [
                'name' => $name,
                'url' => url('', ['pay_no' => $payNo])
            ]
        ]);
        $this->assign('name', $name);
        $this->assign('desc', $desc);
        $this->memberDisplay();
    }

    protected function log() {
        $this->setMeta('充值记录');
        $this->pageInfo['name'] = '充值记录';

        $urlParam = [];
        $where = [];

        $pageLimit = 10;
        $where['A.user_id'] = $this->userInfo['user_id'];

        $model = target('member/MemberRecharge');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'recharge_id desc');

        $this->assign('page', $pageData['html']);
        $this->assign('pageList', $list);
        $this->assign('pageParams', $urlParam);
        $this->memberDisplay();
    }

}